package com.hotel.search.lucene;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Scorer;

import java.io.IOException;

public class GroupCollector extends Collector {
    // 保存分组统计结果
    private GroupField groupField = new GroupField();
    //fieldCache
    private String[] fieldCache;
    //统计字段
    private String field;
    String spliter;
    private int docBase;

    public void setFc(String[] fieldCache) {
        this.fieldCache = fieldCache;
    }

    @Override
    public void setScorer(Scorer scorer) throws IOException {
    }

    @Override
    public void collect(int doc) throws IOException {
        // 添加的GroupField中，由GroupField负责统计每个不同值的数目
        groupField.addValue(fieldCache[doc]);
    }

    @Override
    public boolean acceptsDocsOutOfOrder() {
        return true;
    }

    public GroupField getGroupField() {
        return groupField;
    }

    public void setSpliter(String spliter) {
        this.spliter = spliter;
    }

    public void setF(String field) {
        this.field = field;
    }

    @Override
    public void setNextReader(AtomicReaderContext context) throws IOException {
        this.docBase = context.docBase;
    }

}
